package com.company.manage.system.dao;

import com.company.manage.common.persistence.HibernateDao;
import com.company.manage.common.utils.Exceptions;
import com.company.manage.system.entity.Permission;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by HuangYong on 2016/1/22.
 * desc:权限DAO
 */
@Repository
public class PermissionDao extends HibernateDao<Permission,Integer>{
    private static final String TAG = "PermissionDao";

    /**
     * 查询用户拥有的权限
     * @param userId
     * @return
     */
    public List<Permission> findPermissions (Integer userId){
        StringBuffer sb = new StringBuffer();
        try {
            sb.append("SELECT p.* FROM t_permission p ");
            sb.append("INNER JOIN  t_role_permission rp ON p.ID = rp.PERMISSION_ID ");
            sb.append("INNER JOIN t_role r ON r.id = rp.role_id ");
            sb.append("INNER JOIN t_user_role ur ON ur.role_id = rp.role_id ");
            sb.append("INNER JOIN t_user u ON u.id = ur.user_id ");
            sb.append("WHERE u.id = ?0 ORDER BY p.sort ");

            logger.info("### 权限DAO-查询用户拥有的权限[findPermissions] SQL = "+sb.toString());
            SQLQuery sqlQuery = createSQLQuery(sb.toString(),userId);
            sqlQuery.addEntity(Permission.class);
            List<Permission> list = sqlQuery.list();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * 查询所有的菜单
     * @return
     */
    public List<Permission> findMenus(){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT p.id, p.pid, p.name,p.url,p.icon,p.sort,p.description FROM t_permission p ");
        sb.append("WHERE p.type = 'F' ORDER BY p.sort");

        logger.info("### 权限DAO-查询所有的菜单[findMenus] SQL = "+sb.toString());
        SQLQuery sqlQuery = createSQLQuery(sb.toString());
        sqlQuery.addScalar("id", StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("pid", StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("name", StandardBasicTypes.STRING);
        sqlQuery.addScalar("url", StandardBasicTypes.STRING);
        sqlQuery.addScalar("icon", StandardBasicTypes.STRING);
        sqlQuery.addScalar("sort", StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("description", StandardBasicTypes.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(Permission.class));
        return sqlQuery.list();
    }

    /**
     * 查询用户拥有的菜单权限
     * @param userId
     * @return
     */
    public List<Permission> findMenus(Integer userId){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT p.id, p.pid, p.name,p.url,p.icon,p.sort,p.description FROM t_permission p ");
        sb.append("INNER JOIN t_role_permission rp ON p.id = rp.permission_id ");
        sb.append("INNER JOIN t_role r ON r.id = rp.role_id ");
        sb.append("INNER JOIN t_user_role ur ON ur.role_id = rp.role_id ");
        sb.append("INNER JOIN t_user u ON u.id = ur.user_id ");
        sb.append("WHERE p.type = 'F' AND u.id = ?0 ORDER BY p.sort");

        logger.info("### 权限DAO-查询用户拥有的菜单权限 [findMenus] SQL = "+sb.toString());
        SQLQuery sqlQuery = createSQLQuery(sb.toString(),userId);
        sqlQuery.addScalar("id",StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("pid",StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("name",StandardBasicTypes.STRING);
        sqlQuery.addScalar("url",StandardBasicTypes.STRING);
        sqlQuery.addScalar("icon",StandardBasicTypes.STRING);
        sqlQuery.addScalar("sort",StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("description",StandardBasicTypes.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(Permission.class));
        return sqlQuery.list();
    }

    /**
     * 查询菜单下的操作权限
     * @param pid
     * @return
     */
    public List<Permission> findMenuOperation(Integer pid){
        StringBuffer sb = new StringBuffer();
        try {
            sb.append("SELECT p.id, p.name,p.url,p.perm_code permCode,p.description FROM t_permission p ");
            sb.append("WHERE p.type = 'O' AND p.pid = ?0 ORDER BY p.sort");

            logger.info("### 权限DAO-查询菜单下的操作权限 [findMenuOperation] SQL = "+sb.toString());
            SQLQuery sqlQuery = createSQLQuery(sb.toString(),pid);
            sqlQuery.addScalar("id",StandardBasicTypes.INTEGER);
            sqlQuery.addScalar("name",StandardBasicTypes.STRING);
            sqlQuery.addScalar("url",StandardBasicTypes.STRING);
            sqlQuery.addScalar("permCode",StandardBasicTypes.STRING);
            sqlQuery.addScalar("description",StandardBasicTypes.STRING);
            sqlQuery.setResultTransformer(Transformers.aliasToBean(Permission.class));
            return sqlQuery.list();
        } catch (Exception e) {
            e.printStackTrace();
            throw Exceptions.unchecked(e);
        }
    }

}